/*
 * @User 		@Company 	@Date
 * marcusbui	VNNET24  	May 20, 2011
 */
package tsb.com.html5.controller.common;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.stereotype.Component;

import tsb.com.html5.common.param.GetApplicationCachedDataParam;
import tsb.com.html5.common.service.ICommonService;
import tsb.com.html5.common.util.Constant;
import tsb.com.html5.file.item.FileItem;
import tsb.com.html5.product.item.CategoryItem;
import tsb.com.html5.product.item.ProductItem;

// TODO: Auto-generated Javadoc
/**
 * The Class CacheController.
 */
@Component
public class CacheController{

	/** The servlet context. */
	@Resource
	private ServletContext servletContext;
	
	/** The common service. */
	@Resource(name="CommonService")
	private ICommonService commonService;
	
	/**
	 * Gets the application cached data.
	 *
	 * @return the application cached data
	 */
	private HashMap<String, Object> getApplicationCachedData() {
		return (HashMap<String, Object>)servletContext.getAttribute(Constant.APP_CACHE);
	}

	/**
	 * Sets the application cached data.
	 *
	 * @param applicationCachedData the application cached data
	 */
	private void setApplicationCachedData(HashMap<String, Object> applicationCachedData) {
		servletContext.setAttribute(Constant.APP_CACHE, applicationCachedData);
	}

	/**
	 * Load application cache.
	 *
	 * @throws Exception the exception
	 */
	public void loadApplicationCache() throws Exception{
		GetApplicationCachedDataParam param = new GetApplicationCachedDataParam();
		HashMap<String, Object> applicationData = commonService.getApplicationCachedData(param);
		setApplicationCachedData(applicationData);
	}
	
	/**
	 * Gets the category.
	 *
	 * @return the category
	 * @throws Exception the exception
	 */
	public List<CategoryItem> getCategory() throws Exception{
		List<CategoryItem> categoryList = null;
		try {
			categoryList =  (List<CategoryItem>)getApplicationCachedData().get(Constant.CATEGORY);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return categoryList;
	}
	
	/**
	 * Gets the file.
	 *
	 * @return the file
	 * @throws Exception the exception
	 */
	public List<FileItem> getFile() throws Exception{
		List<FileItem> list = null;
		try {
			list =  (List<FileItem>)getApplicationCachedData().get(Constant.FILE_LIST);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return list;
	}
	
	/**
	 * Gets the product.
	 *
	 * @return the product
	 * @throws Exception the exception
	 */
	public List<ProductItem> getProduct() throws Exception{
		List<ProductItem> list = null;
		try {
			list =  (List<ProductItem>)getApplicationCachedData().get(Constant.PRODUCT_LIST);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return list;
	}
	
}
